trigger SalarySharingTrigger on Salary__c (after insert) {

	//TODO: Instantiate two salary lists called "mgrRecords" and "vpRecords"
	List<Salary__c> mgrRecords = new List<Salary__c>();
	List<Salary__c> vpRecords = new List<Salary__c>();

	//TODO: Instantiate a list of the triggering salaries called "triggerRecords" which includes the following fields: position, hiring manager, department
	List<Salary__c> triggerRecords = [select id, position__c, position__r.hiring_manager__c, position__r.department__c from salary__c where id IN :Trigger.newMap.keySet()];

	//TODO: Loop through the triggering salary records
	for(Salary__c salary:triggerRecords){

		//TODO: Within that loop, verify that the position is not null (if it is, add an error because the position should be required)
		if (salary.position__c != null){
			//TODO: If the position has a hiring manager listed, add this salary record to the mgrRecords list
			if(salary.position__r.hiring_manager__c != null){
				mgrRecords.add(salary);
			}
			if(salary.position__r.department__c != null){
				vpRecords.add(salary);
			}

			//TODO: If the position has a department listed, add this salary record to the vpRecords list
		}
		else {
			// salary.position__c.addError('Position is a required field');
			salary.position__c.addError('要求済みの募集職種です。');
		}

	//TODO: Close the loop.
	}

	//TODO: If the mgrRecords list has records, call the approrpriate method in the SalarySharing class to add sharing for this record
	if( mgrRecords != null && mgrRecords.size() > 0 ){
		SalarySharing.addSharing(mgrRecords, 'Hiring_Manager__c','Edit');
	}

	//TODO: Do the same for vpRecords
	if( vpRecords != null && vpRecords.size() > 0 ){
		SalarySharing.addSharing(vpRecords, 'Department_VP__c','Edit');
	}

}